package com.damocode.starter.yx.http;

import com.damocode.starter.httpclient.domain.UrlEncodedFormEntityRequest;
import com.damocode.starter.httpclient.helper.MethodType;
import com.damocode.starter.yx.utils.CheckSumBuilder;

import java.util.UUID;

public abstract class YxRequest extends UrlEncodedFormEntityRequest {

    private String appKey;

    private String appSecret;

    public YxRequest(String url,String appKey,String appSecret) {
        super(url);
        this.appKey = appKey;
        this.appSecret = appSecret;
        initialize();
    }

    private void initialize() {
        this.setMethod(MethodType.POST);
        this.setEncoding("UTF-8");
        setRequestHeader();
    }

    private void setRequestHeader(){
        String nonce = UUID.randomUUID().toString().replace("-", "");
        String curTime = String.valueOf(System.currentTimeMillis() / 1000);
        this.putHeaderParameter("AppKey",appKey);
        this.putHeaderParameter("Nonce",nonce);
        this.putHeaderParameter("CurTime",curTime);
        this.putHeaderParameter("CheckSum", CheckSumBuilder.getCheckSum(appSecret,nonce,curTime));
    }

}
